#include "ItemBlock.h"


ItemBlock::ItemBlock(IDITem TypeItem, float x, float y) : ITem(x,y)
{
	m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_ItemBLock);
	m_TimeAnimation = ItemTimeAnimation;
	m_Gametime = new GameTime();
	m_Explode = new Explode();
	m_Explode->Init(IDImage::IMG_MachineExplode);

	m_Status = IDStatus::STAND;

	m_Vpx = 0;
	m_Vpy = VIEW_PORT_Y;
	m_Index = 0;
	m_temp = 1;

	m_statusITem = ItemStatus::PROTECTED;
	m_TypeItems = TypeItem;
}

void ItemBlock::UpdateEffect()
{
	m_Gametime->setTime();
	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		m_Gametime->Update();

		m_Explode->Next();

		if (m_Explode->NoneEffect())
		{
			//m_Status = IDStatus::DIE;
			m_IsEffect = false;
			if (m_statusITem == ItemStatus::CAN_BREAK)
			{
				m_statusITem = ItemStatus::CAN_GIVE;
				m_Sprite = Source::getInstance()->getSprite(IDImage::IMG_ItemGun);
				m_Index = m_TypeItems;
				m_Sprite->setIndex(m_Index);
				m_TimeJump = 6 * m_TimeAnimation;
			}
		}
	}
}

void ItemBlock::UpdateAnimationBlock()
{
	m_Gametime->setTime();

	if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation)
	{
		if (m_Sprite->getCurrentIndex() >= 4)
		{
			m_statusITem = ItemStatus::CAN_BREAK;
		}
		else
			m_statusITem = ItemStatus::PROTECTED;

		m_Index += m_temp;
		if (m_Index >= m_Sprite->getCount() - 1)
		{
			m_temp *= -1;
		}

		if (m_Index <= 0)
		{
			m_temp *= -1;
			m_TimeAnimation *= 10;// tai vi tri index = 0 se cho thoi gian dung 
		}
		else
			m_TimeAnimation = ItemTimeAnimation;

		m_Sprite->setIndex(m_Index);
		m_Gametime->Update();
	}
}

void ItemBlock::Update()
{
	if (m_IsEffect)
	{
		UpdateEffect();
	}
	else if (m_statusITem == ItemStatus::PROTECTED || m_statusITem == ItemStatus::CAN_BREAK)
		UpdateAnimationBlock();
	else if (m_statusITem == ItemStatus::CAN_GIVE)
	{
		m_Gametime->setTime();

		if (m_Gametime->getElapsedTimePerSecond() >= m_TimeAnimation / 2)
		{
			m_Gametime->Update();
			m_Veclocity_vy = VECLOCITY_VY;
			m_Veclocity_vx = -SPEED / 1.5;

			m_Position.x += m_Veclocity_vx;
			if (m_TimeJump > 0)
			{
				m_TimeJump -= m_TimeAnimation;
				m_Position.y += m_Veclocity_vy;
			}
			else
			{
				m_Position.y -= FALLDOWN_VELOCITY_DECREASE / 1.5;
			}
		}
	}
}



ItemBlock::~ItemBlock()
{
}
